package com.emqx.auth.controller;

import com.emqx.auth.dto.EmqxAuthDTO;
import com.emqx.auth.enums.AuthType;
import com.emqx.auth.service.EmqxAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.Executor;

/**
 * @author :  zhangfutao
 * @version :  V-1.0
 * @description :
 * @date :   2021/12/17 11:42
 */
@Slf4j
@RestController
public class HttpAuthController {

    @Resource
    private EmqxAuthService emqxAuthService;
    @Resource
    private Executor executor;

    @PostMapping("/mqtt/auth")
    public String push(@RequestParam("clientid") String clientid,
                       @RequestParam(value = "username") String username,
                       @RequestParam(value = "password", required = false) String password,
                       @RequestParam(value = "ipaddr", required = false) String ipAddr,
                       @RequestParam(value = "protocol", required = false) String protocol,
                       HttpServletResponse response) {
        executor.execute(() -> {
            log.info("【接收到mqtt连接请求】: 【clientid:{}】, 【username:{}】, 【password:{}】, 【ip:{}】", clientid, username, password, ipAddr);
            EmqxAuthDTO emqxAuthDTO = EmqxAuthDTO.builder()
                    .authType(AuthType.AUTH.value)
                    .clientId(clientid)
                    .username(username)
                    .password(password)
                    .ipAddr(ipAddr)
                    .protocol(protocol)
                    .connectTime(new Date())
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
            emqxAuthService.add(emqxAuthDTO);
        });
        response.setStatus(200);
        return "ok";
    }

    @PostMapping("/mqtt/superuser")
    public String superuser(@RequestParam("clientid") String clientid,
                            @RequestParam(value = "username") String username,
                            @RequestParam(value = "password", required = false) String password,
                            @RequestParam(value = "ipaddr", required = false) String ipAddr,
                            @RequestParam(value = "protocol", required = false) String protocol,
                            HttpServletResponse response) {
        executor.execute(() -> {
            log.info("【接收到mqtt超级用户连接请求】: 【clientid:{}】, 【username:{}】, 【password:{}】, 【ip:{}】", clientid, username, password, ipAddr);
            EmqxAuthDTO emqxAuthDTO = EmqxAuthDTO.builder()
                    .authType(AuthType.SUPERUSER.value)
                    .clientId(clientid)
                    .username(username)
                    .password(password)
                    .ipAddr(ipAddr)
                    .protocol(protocol)
                    .connectTime(new Date())
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
            emqxAuthService.add(emqxAuthDTO);
        });
        response.setStatus(200);
        return "ok";
    }

    @PostMapping("/mqtt/acl")
    public String acl(@RequestParam("access") String access,
                      @RequestParam(value = "username") String username,
                      @RequestParam(value = "clientid") String clientid,
                      @RequestParam(value = "ipaddr") String ipaddr,
                      @RequestParam(value = "topic") String topic,
                      @RequestParam(value = "mountpoint") String mountpoint,
                      HttpServletResponse response) {
        executor.execute(() -> {
            String s = "";
            if ("1".equals(access)) {
                s = "订阅";
            } else {
                s = "发布";
            }
            log.info("【接收到mqtt" + s + "请求】: 【账号】:" + username + ",【clienid】:" + clientid + ",【ip】:" + ipaddr + ",【主题】:" + topic);
        });
        response.setStatus(200);
        return "ok";
    }
}
